package cn.nawang.ebeim.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.nawang.ebeim.api.domain.StorageVersionTreeToDo;
import cn.nawang.ebeim.api.repository.StorageVersionTreeToDoRepository;
import cn.nawang.ebeim.api.repository.impl.StorageVersionTreeToDoRepositoryImpl;
import cn.nawang.ebeim.api.service.StorageVersionTreeToDoService;
import cn.nawang.ebeim.api.utils.Constant;
import cn.nawang.ebeim.api.utils.ConvertUtil;
import cn.nawang.ebeim.api.vo.StorageVersionTreeToDoView;

/**
 * @ClassName: StorageVersionTreeToDoServiceImpl
 * @Description: TODO
 * @author fanyb
 * @date Aug 26, 2015 2:50:21 PM
 * @version v1.0
 */
@Service("storageVersionTreeToDoService")
public class StorageVersionTreeToDoServiceImpl implements StorageVersionTreeToDoService {

  @Autowired
  private StorageVersionTreeToDoRepository storageVersionTreeToDoRepository;

  @Autowired
  private StorageVersionTreeToDoRepositoryImpl storageVersionTreeToDoRepositoryImpl;

  @Override
  public boolean add(StorageVersionTreeToDo storageVersionTreeToDo) {
    try {
      storageVersionTreeToDoRepository.save(storageVersionTreeToDo);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public List<StorageVersionTreeToDoView> findUntreatedList() {
    List<StorageVersionTreeToDoView> list = storageVersionTreeToDoRepositoryImpl
        .findAll(Constant.QUERY_COUNT, Constant.STORAGE_VERSION_TREE_TO_DO_STATUS_NO);
    // StringBuffer updateIds=new StringBuffer();
    // if(!list.isEmpty()){
    // for(int i=0;i<list.size();i++){
    // StorageVersionTreeToDoView storageVersionTreeToDoView=list.get(i);
    // updateIds.append(storageVersionTreeToDoView.getId());
    // updateIds.append(",");
    // }
    // String ids=updateIds.toString();
    // String aa=ids.substring(0,ids.lastIndexOf(","));
    // String[] id=aa.split(",");
    // storageVersionTreeToDoRepositoryImpl.update(ConvertUtil.toDbString(id));
    // }
    return list;
  }

  @Override
  public boolean delete(String[] ids) {
    try {
      return storageVersionTreeToDoRepositoryImpl.delete(ConvertUtil.toDbString(ids));
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public boolean update(StorageVersionTreeToDo storageVersionTreeToDo) {
    try {
      storageVersionTreeToDoRepository.save(storageVersionTreeToDo);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public boolean isExistStorageVersionTreeToDo(String storageCloudId, String datasourceId,
      String version) {
    List<StorageVersionTreeToDo> list =
        storageVersionTreeToDoRepositoryImpl.findByProperty(storageCloudId, datasourceId, version);
    if (!list.isEmpty() && list != null) {
      return true;
    }
    return false;
  }

  @Override
  public StorageVersionTreeToDo findByDatasourceIdAndVersion(String datasourceId, String version) {
    StorageVersionTreeToDo storageVersionTreeToDo =
        storageVersionTreeToDoRepositoryImpl.findByDatasourceIdAndVersion(datasourceId, version);
    return storageVersionTreeToDo;
  }

}
